/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.ad.domain.SysOrganizationalRole;
import cn.ibizlab.core.ad.service.SysOrganizationalRoleService;
import cn.ibizlab.core.ad.filter.SysOrganizationalRoleSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[SysOrganizationalRole] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysOrganizationalRoleResource {

    @Autowired
    public SysOrganizationalRoleService sysOrganizationalRoleService;

    @Autowired
    @Lazy
    public SysOrganizationalRoleDTOMapping sysOrganizationalRoleDtoMapping;

    @Autowired
    @Lazy
    public SysOrganizationalRoleFilterDTOMapping sysOrganizationalRoleFilterDtoMapping;


    /**
    * 创建Create 用户组
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysOrganizationalRoleDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"用户组" },  notes = "SysOrganizationalRole-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-Create-all') or hasPermission(this.sysOrganizationalRoleDtoMapping.toDomain(#dto),'uaa-SysOrganizationalRole-Create')")
    @PostMapping(value = {"sys_organizational_roles","sys_organizational_roles"})
    public ResponseEntity<SysOrganizationalRoleDTO> create
            (@Validated @RequestBody SysOrganizationalRoleDTO dto) {
        SysOrganizationalRole domain = sysOrganizationalRoleDtoMapping.toDomain(dto);
        sysOrganizationalRoleService.create(domain);
        SysOrganizationalRole rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalRoleDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 用户组
    * 
    *
    * @param id id
    * @return ResponseEntity<SysOrganizationalRoleDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"用户组" },  notes = "SysOrganizationalRole-Get ")
    @PostAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-Get-all') or hasPermission(this.sysOrganizationalRoleDtoMapping.toDomain(returnObject.body),'uaa-SysOrganizationalRole-Get')")
    @GetMapping("sys_organizational_roles/{id}")
    public ResponseEntity<SysOrganizationalRoleDTO> getById
            (@PathVariable("id") String id) {
        SysOrganizationalRole rt = sysOrganizationalRoleService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalRoleDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 用户组
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"用户组" },  notes = "SysOrganizationalRole-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-Remove-all') or hasPermission(this.sysOrganizationalRoleService.get(#id),'uaa-SysOrganizationalRole-Remove')")
    @DeleteMapping("sys_organizational_roles/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysOrganizationalRoleService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 用户组
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysOrganizationalRoleDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"用户组" },  notes = "SysOrganizationalRole-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-Update-all') or hasPermission(this.sysOrganizationalRoleService.get(#id),'uaa-SysOrganizationalRole-Update')")
    @VersionCheck(entity = "sysorganizationalrole" , versionfield = "updateTime")
    @PutMapping("sys_organizational_roles/{id}")
    public ResponseEntity<SysOrganizationalRoleDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysOrganizationalRoleDTO dto) {
        SysOrganizationalRole domain = sysOrganizationalRoleDtoMapping.toDomain(dto);
        domain.setId(id);
        sysOrganizationalRoleService.update(domain);
        SysOrganizationalRole rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalRoleDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 用户组
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"用户组" },  notes = "SysOrganizationalRole-CheckKey ")
    @PostMapping("sys_organizational_roles/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysOrganizationalRoleDTO dto) {
        SysOrganizationalRole domain = sysOrganizationalRoleDtoMapping.toDomain(dto);
        Integer rt = sysOrganizationalRoleService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 用户组
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysOrganizationalRoleDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"用户组" },  notes = "SysOrganizationalRole-GetDraft ")
    @GetMapping("sys_organizational_roles/getdraft")
    public ResponseEntity<SysOrganizationalRoleDTO> getDraft
            (@SpringQueryMap SysOrganizationalRoleDTO dto) {
        SysOrganizationalRole domain = sysOrganizationalRoleDtoMapping.toDomain(dto);
        SysOrganizationalRole rt = sysOrganizationalRoleService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalRoleDtoMapping.toDto(rt));
    }

    /**
    * RoleAuth 用户组
    * 批量授权系统角色
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysOrganizationalRoleDTO>
    */
    @ApiOperation(value = "RoleAuth", tags = {"用户组" },  notes = "SysOrganizationalRole-RoleAuth 批量授权系统角色")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-RoleAuth-all') or hasPermission(this.sysOrganizationalRoleDtoMapping.toDomain(#dto),'uaa-SysOrganizationalRole-RoleAuth')")
    @PostMapping("sys_organizational_roles/{id}/roleauth")
    public ResponseEntity<SysOrganizationalRoleDTO> roleAuthById
            (@PathVariable("id") String id, @Validated @RequestBody SysOrganizationalRoleDTO dto) {
        SysOrganizationalRole domain = sysOrganizationalRoleDtoMapping.toDomain(dto);
        domain.setId(id);
        SysOrganizationalRole rt = sysOrganizationalRoleService.roleAuth(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalRoleDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 用户组
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysOrganizationalRoleDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"用户组" },  notes = "SysOrganizationalRole-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-Save-all') or hasPermission(this.sysOrganizationalRoleDtoMapping.toDomain(#dto),'uaa-SysOrganizationalRole-Save')")
    @PostMapping("sys_organizational_roles/save")
    public ResponseEntity<SysOrganizationalRoleDTO> save
            (@Validated @RequestBody SysOrganizationalRoleDTO dto) {
        SysOrganizationalRole domain = sysOrganizationalRoleDtoMapping.toDomain(dto);
        sysOrganizationalRoleService.save(domain);
        SysOrganizationalRole rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalRoleDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchCurPersonGroup 用户组
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationalRoleDTO>>
    */
    @ApiOperation(value = "查询FetchCurPersonGroup", tags = {"用户组" },  notes = "SysOrganizationalRole-FetchCurPersonGroup ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-FetchCurPersonGroup-all') or hasPermission(#dto,'uaa-SysOrganizationalRole-FetchCurPersonGroup')")
    @PostMapping("sys_organizational_roles/fetchcurpersongroup")
    public ResponseEntity<List<SysOrganizationalRoleDTO>> fetchCurPersonGroup
            (@Validated @RequestBody SysOrganizationalRoleFilterDTO dto) {
        SysOrganizationalRoleSearchContext context = sysOrganizationalRoleFilterDtoMapping.toDomain(dto);
        Page<SysOrganizationalRole> domains = sysOrganizationalRoleService.searchCurPersonGroup(context) ;
        List<SysOrganizationalRoleDTO> list = sysOrganizationalRoleDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchDefault 用户组
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysOrganizationalRoleDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"用户组" },  notes = "SysOrganizationalRole-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-FetchDefault-all') or hasPermission(#dto,'uaa-SysOrganizationalRole-FetchDefault')")
    @PostMapping("sys_organizational_roles/fetchdefault")
    public ResponseEntity<List<SysOrganizationalRoleDTO>> fetchDefault
            (@Validated @RequestBody SysOrganizationalRoleFilterDTO dto) {
        SysOrganizationalRoleSearchContext context = sysOrganizationalRoleFilterDtoMapping.toDomain(dto);
        Page<SysOrganizationalRole> domains = sysOrganizationalRoleService.searchDefault(context) ;
        List<SysOrganizationalRoleDTO> list = sysOrganizationalRoleDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建用户组
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-Create-all')")
    @ApiOperation(value = "批量新建用户组", tags = {"用户组" },  notes = "批量新建用户组")
	@PostMapping("sys_organizational_roles/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysOrganizationalRoleDTO> dtos) {
        sysOrganizationalRoleService.createBatch(sysOrganizationalRoleDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除用户组
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-Remove-all')")
    @ApiOperation(value = "批量删除用户组", tags = {"用户组" },  notes = "批量删除用户组")
	@DeleteMapping("sys_organizational_roles/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysOrganizationalRoleService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新用户组
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-Update-all')")
    @ApiOperation(value = "批量更新用户组", tags = {"用户组" },  notes = "批量更新用户组")
	@PutMapping("sys_organizational_roles/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysOrganizationalRoleDTO> dtos) {
        sysOrganizationalRoleService.updateBatch(sysOrganizationalRoleDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存用户组
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-Save-all')")
    @ApiOperation(value = "批量保存用户组", tags = {"用户组" },  notes = "批量保存用户组")
	@PostMapping("sys_organizational_roles/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysOrganizationalRoleDTO> dtos) {
        sysOrganizationalRoleService.saveBatch(sysOrganizationalRoleDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入用户组
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysOrganizationalRole-Save-all')")
    @ApiOperation(value = "批量导入用户组", tags = {"用户组" },  notes = "批量导入用户组")
	@PostMapping("sys_organizational_roles/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysOrganizationalRoleDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysOrganizationalRoleService.importData(config,ignoreError,sysOrganizationalRoleDtoMapping.toDomain(dtos)));
    }

}
